package com.googlecode.iiddefecttracker.web;

import org.springframework.validation.Errors;

import com.googlecode.iiddefecttracker.domain.Project;
import com.googlecode.iiddefecttracker.domain.ProjectBeanImpl;

/**
 * default implementation of {@link ProjectFormValidator}.
 */
public class ProjectFormValidatorImpl implements ProjectFormValidator {
    
    @SuppressWarnings("unchecked")
    public boolean supports(final Class givenClass) {
        return givenClass.equals(ProjectBeanImpl.class);
        
    }
    
    public void validate(final Object objectToValidate, final Errors errors) {
        
        if (objectToValidate == null) {
            errors.reject("error.nullpointer", "Null data received");
        }
        
        final Project project = (Project) objectToValidate;
        
        if (project.getName().trim().length() <= 0) {
            errors.rejectValue("name", "error.code", "name must have a value");
        }
        
        if (project.getVersion().trim().length() <= 0) {
            errors.rejectValue("version", "error.code",
                    "version must have a value");
        }
        
        if (project.getDescription().trim().length() <= 0) {
            errors.rejectValue("description", "error.code",
                    "description must have a value");
        }
    }
}
